#include "HammurabiNet.h"
#include "ClientInterface.h"
#include <stdio.h>
#include <iostream>
using namespace std;

#define BUF_LEN 80

int clientInterface::init()
{
	// set default values
	// the information has not been updated
	hasBeenUpdated = false;
	// set input values to 0
	totalDeaths = currentSales = currentFeed = currentPlant = 0;
	lastMessage.setVoid();
	return 0;
}

clientInterface::clientInterface()
{
	hasBeenUpdated = true;
	// set values to zero
	totalDeaths = currentSales = currentFeed = currentPlant = 0;
}

clientInterface::~clientInterface()
{
	//delete hammurabiNet::getInstance();
}

int clientInterface::update()
{
	if(lastMessage.isEmpty())
		return 1;
	// clear the screen
	system("CLS");
	// check if a message from the server has come
	if(!hasBeenUpdated) // return
	{
		printf_s("calculating...");
		return 1;
	}

	// check if game has been lost
	if(lastMessage.pop == 0)
	{
		printf_s("Your entire population has died!\nYou are a horrible ruler!\n");
		return 0;
	}

	// check if the game is over
	if(lastMessage.year > 10)
	{
		// calculate number of acres per person
		float acresPerPop = (float)lastMessage.acres/(float)lastMessage.pop;
		printf_s("The Game is Over!\nYou ended with %f acres per person.\n%d people died!\n\n",acresPerPop,totalDeaths);
		// calculate percent of people that died
		float percentDead = (float)totalDeaths/(float)lastMessage.pop;
		// bad ending
		if(percentDead > .33f || acresPerPop < 7.0f)
		{
			printf_s("You are a horrible leader and have been impeached!\n");
		}
		// meh ending
		else if(percentDead > .10f || acresPerPop < 9.0f)
		{
			printf_s("Your people hate you!");
		}
		// good ending
		else if(percentDead > .03f || acresPerPop <= 10.0f)
		{
			printf_s("A FANTASTIC PERFORMANCE!!!\n CHARLEMANGE, DISRAELI, AND JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!");
		}
		return 0;
	}

	// output buffer
	char buffer[BUF_LEN] = {0};
	// input variables
	char inputChoice = 0;
	int inputAmount = 0;

	// display info
	printInfo();
	// display choices
	printChoices();
	// get input choice from the player
	char input[BUF_LEN];
	cin >> input;
	// quiting
	if(input[0] == 'q' || input[0] == 'Q')
		return 0;
	// debugg for adding year
	if(input[0] == 'k' || input[0] == 'K')
		lastMessage.year = 9;
	// server messages
	if(hammurabiNet::getInstance()->input(input) == 1)
		return 1;
	// process input
	handleInput(input[0]);
	// if player decided to submit input then send info to the server
	return 1;
}

void clientInterface::clientMessage(msg a_msg)
{
	// the information has been updated
	hasBeenUpdated = true;
	// store the latest message
	lastMessage = a_msg;
	// save the current amount of bushels
	bushelsRemaining = lastMessage.bushels;
	// save deaths
	totalDeaths += lastMessage.starved;
	// reset temp values
	currentFeed = currentPlant = currentSales = 0;
}

void clientInterface::printInfo()
{

	// output buffer
	char buffer[BUF_LEN] = {0};

	printf_s("*******************************************************************************\nHammurabi: I beg to report to you,\n");
	//In Year X, Y people starved.
	sprintf_s(buffer,"In Year %d, %d people starved.",lastMessage.year, lastMessage.starved);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	// If a plague accured report it
	if(lastMessage.plague)
	{
		printf_s("A horrible plague struck! Half the population died.\n");
	}
	//Z people came to the city
	sprintf_s(buffer,"%d people came to the city.",lastMessage.newbs);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	//The city's population is now A.
	sprintf_s(buffer,"The city's population is now %d.",lastMessage.pop);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	//The city now owns B acres.
	sprintf_s(buffer,"The city now owns %d acres.",lastMessage.acres);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	//You harvested C bushels per acre.
	sprintf_s(buffer,"You harvested %d bushels per acre.",lastMessage.farmed);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	//Rats ate D bushels.
	sprintf_s(buffer,"Rats ate %d bushels.",lastMessage.rats);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	//You now have E bushels in store. V bushels remaining
	sprintf_s(buffer,"You now have %d bushels in store. %d bushels remaining",lastMessage.bushels,bushelsRemaining);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	//Land is trading at F bushels per acre.
	sprintf_s(buffer,"Land is trading at %d bushels per acre.",lastMessage.bpa);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	// display a divide
	printf_s("*******************************************************************************\n");
	// print the amount being bought/sold
	sprintf_s(buffer,"Acres to be bought/sold: %d",currentSales);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	// print amount to be feed
	sprintf_s(buffer,"Bushels to be given to your people: %d",currentFeed);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	// print amount to be worked
	sprintf_s(buffer,"Acres to be worked: %d",currentPlant);
	printf_s("%s\n",buffer);
	ZeroMemory(buffer,BUF_LEN);
	printf_s("*******************************************************************************\n");
}

void clientInterface::printChoices()
{
	//S : to enter the amount of acres sold/bought
	//F : to enter the amount of bushels for your people
	//P : to enter the amount of acres to be worked
	//M : Make it so!
	//::
	printf_s("S : to enter the amount of acres to sell/buy\nF : to enter the amount of bushels for your people.\nP : to enter the amount of acres to be worked.\nM : Make it so! *end the year\nQ : Quit\n::");
}

void clientInterface::handleInput(char a_input)
{
	char testBuff[100] = {0};
	bool correct = true;
	switch(a_input)
	{
	case 'S':
	case 's':
		printf_s("Enter the amount of acres to sell/buy.\nuse positve numbers buy acres and negative sell them\n::");
		cin >> testBuff;
		// make sure that the entered value is valid
		currentSales = atoi(testBuff);
		correct = false;
		do
		{
			// make sure they are not selling more acres than they own
			if(currentSales < -lastMessage.acres)
			{
				system("CLS");
				printInfo();
				printf_s("You can not sell more acres than you own please re-enter the amount.\n::");
				ZeroMemory(testBuff,100);
				cin >> testBuff;
				currentSales = atoi(testBuff);
				correct = false;
			}
			else
				correct = true;
		}while(!correct);
		break;
	case 'F':
	case 'f':
		printf_s("Enter the amount of bushels for your people.\nYour People eat 20 bushels each\n::");
		cin >> testBuff;
		currentFeed = atoi(testBuff);
		correct = false;
		do
		{
			if(currentFeed < 0)
			{
				system("CLS");
				printInfo();
				printf_s("You can not take bushels from your people please re-enter the amount\n::");
				ZeroMemory(testBuff,100);
				cin >> testBuff;
				currentFeed = atoi(testBuff);
				correct = false;
			}
			else
				correct = true;
		}while(!correct);
		break;
	case 'P':
	case 'p':
		printf_s("Enter the amount of acres to be worked.\nYour people can only 10 acres each\n::");
		cin >> testBuff;
		currentPlant = atoi(testBuff);
		correct = false;
		do
		{
			if(currentPlant > lastMessage.acres + currentSales || currentPlant > ((lastMessage.pop) * 10) || currentPlant < 0)
			{
				system("CLS");
				printInfo();
				printf_s("Your can not plant more than you are capable of please re-enter the amount\n::");
				ZeroMemory(testBuff,100);
				cin >> testBuff;
				currentPlant = atoi(testBuff);
				correct = false;
			}
			else
				correct = true;
		}while(!correct);
		break;
	};
	// recalculate the amount of bushels
	bushelsRemaining = lastMessage.bushels;
	bushelsRemaining -= (currentSales * lastMessage.bpa);
	bushelsRemaining -= currentPlant;
	bushelsRemaining -= currentFeed;
	// now check if the player wishes to commit their choices
	if(a_input == 'M' || a_input == 'm')
	{
		// first check that bushels remaining is above 0
		if(bushelsRemaining >= 0)
		{
			hasBeenUpdated = false;
			msg sendMsg;
			sendMsg = lastMessage;
			sendMsg.buysell = currentSales;
			sendMsg.feed = currentFeed;
			sendMsg.plant = currentPlant;
			hammurabiNet::getInstance()->serverMsg(sendMsg);
		}
		else
		{
			// clear the screen
			system("CLS");
			printInfo();
			printf_s("You can not end the year with less than 0 bushels!\n");
			_getch();
		}
	}
}